home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
-
- PROGRAM: win_clnt.c
-
- PURPOSE: sample Kerberos client for Windows
-
- FUNCTIONS:
-
- WinMain() - calls initialization function, processes message loop
- InitApplication() - initializes window data and registers window
- InitInstance() - saves instance handle and creates main window
- MainWndProc() - processes messages
- About() - processes messages for "About" dialog box
-
- COMMENTS:
-
- Windows can have several copies of your application running at the
- same time. The variable hInst keeps track of which instance this
- application is so that processing will be to the correct window.
-
- ****************************************************************************/
-
- #include "windows.h" /* required for all Windows applications */
- #include "win_clnt.h" /* specific to this program */
-
- #include <mit_copy.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <krb.h>
-
- HANDLE hInst; /* current instance */
- char hostname[100];
- int checksum;
- long FAR PASCAL MainWndProc();
- BOOL FAR PASCAL Dialog();
- void authenticate(HWND,char*,long);
- int far printf();
- static int sock=0;
-
- int stderr=2;
- /****************************************************************************
-
- FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
-
- PURPOSE: calls initialization function, processes message loop
-
- COMMENTS:
-
- Windows recognizes this function by name as the initial entry point
- for the program. This function calls the application initialization
- routine, if no other instance of the program is running, and always
- calls the instance initialization routine. It then executes a message
- retrieval and dispatch loop that is the top-level control structure
- for the remainder of execution. The loop is terminated when a WM_QUIT
- message is received, at which time this function exits the application
- instance by returning the value passed by PostQuitMessage().
-
- If this function must abort before entering the message loop, it
- returns the conventional value NULL.
-
- ****************************************************************************/
-
- int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
- HANDLE hInstance; /* current instance */
- HANDLE hPrevInstance; /* previous instance */
- LPSTR lpCmdLine; /* command line */
- int nCmdShow; /* show-window type (open/icon) */
- {
- MSG msg; /* message */
-
- if (!hPrevInstance) /* Other instances of app running? */
- if (!InitApplication(hInstance)) /* Initialize shared things */
- return (FALSE); /* Exits if unable to initialize */
-
- /* Perform initializations that apply to a specific instance */
-
- if (!InitInstance(hInstance, nCmdShow))
- return (FALSE);
-
- /* Acquire and dispatch messages until a WM_QUIT message is received. */
-
- while (GetMessage(&msg, /* message structure */
- NULL, /* handle of window receiving the message */
- NULL, /* lowest message to examine */
- NULL)) /* highest message to examine */
- {
- TranslateMessage(&msg); /* Translates virtual key codes */
- DispatchMessage(&msg); /* Dispatches message to window */
- }
- return (msg.wParam); /* Returns the value from PostQuitMessage */
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitApplication(HANDLE)
-
- PURPOSE: Initializes window data and registers window class
-
- COMMENTS:
-
- This function is called at initialization time only if no other
- instances of the application are running. This function performs
- initialization tasks that can be done once for any number of running
- instances.
-
- In this case, we initialize a window class by filling out a data
- structure of type WNDCLASS and calling the Windows RegisterClass()
- function. Since all instances of this application use the same window
- class, we only need to do this when the first instance is initialized.
-
-
- ****************************************************************************/
-
- BOOL InitApplication(hInstance)
- HANDLE hInstance; /* current instance */
- {
- WNDCLASS wc;
-
- /* Fill in window class structure with parameters that describe the */ /* main window. */
-
- wc.style = NULL; /* Class style(s). */
- wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
- /* windows of this class. */
- wc.cbClsExtra = 0; /* No per-class extra data. */
- wc.cbWndExtra = 0; /* No per-window extra data. */
- wc.hInstance = hInstance; /* Application that owns the class. */
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "Main_Menu"; /* Name of menu resource in .RC file. */
- wc.lpszClassName = "win_clntWClass"; /* Name used in call to CreateWindow. */
-
- /* Register the window class and return success/failure code. */
-
- return (RegisterClass(&wc));
-
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitInstance(HANDLE, int)
-
- PURPOSE: Saves instance handle and creates main window
-
- COMMENTS:
-
- This function is called at initialization time for every instance of
- this application. This function performs initialization tasks that
- cannot be shared by multiple instances.
-
- In this case, we save the instance handle in a static variable and
- create and display the main program window.
-
- ****************************************************************************/
-
- BOOL InitInstance(hInstance, nCmdShow)
- HANDLE hInstance; /* Current instance identifier. */
- int nCmdShow; /* Param for first ShowWindow() call. */
- {
- HWND hWnd; /* Main window handle. */
-
- /* Save the instance handle in static variable, which will be used in */
- /* many subsequence calls from this application to Windows. */
-
- hInst = hInstance;
-
-
- /* Create a main window for this application instance. */
-
- hWnd = CreateWindow(
- "win_clntWClass", /* See RegisterClass() call. */
- "Kerberos Sample Client", /* Text for window title bar. */
- WS_OVERLAPPEDWINDOW, /* Window style. */
- CW_USEDEFAULT, /* Default horizontal position. */
- CW_USEDEFAULT, /* Default vertical position. */
- CW_USEDEFAULT, /* Default width. */
- CW_USEDEFAULT, /* Default height. */
- NULL, /* Overlapped windows have no parent. */
- NULL, /* Use the window class menu. */
- hInstance, /* This instance owns this window. */
- NULL /* Pointer not needed. */
- );
-
- /* If window could not be created, return "failure" */
-
- if (!hWnd)
- return (FALSE);
-
- /* Make the window visible; update its client area; and return "success" */
-
- ShowWindow(hWnd, nCmdShow); /* Show the window */
- UpdateWindow(hWnd); /* Sends WM_PAINT message */
- return (TRUE); /* Returns the value from PostQuitMessage */
-
- }
-
- /****************************************************************************
-
- FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages
-
- MESSAGES:
-
- WM_COMMAND - application menu (About dialog box)
- WM_DESTROY - destroy window
-
- COMMENTS:
-
- To process the IDM_ABOUT message, call MakeProcInstance() to get the
- current instance address of the About() function. Then call Dialog
- box which will create the box according to the information in your
- win_clnt.rc file and turn control over to the About() function. When
- it returns, free the intance address.
-
- ****************************************************************************/
-
- long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
- HWND hWnd; /* window handle */
- unsigned message; /* type of message */
- WORD wParam; /* additional information */
- LONG lParam; /* additional information */
- {
- FARPROC lpProcDialog; /* pointer to the "Dialog" function */
-
- switch (message) {
- case WM_COMMAND: /* message: command from application menu */
- if (wParam == IDM_GO) {
- /* set_debug_window(hWnd); */
- lpProcDialog = MakeProcInstance(Dialog, hInst);
-
- DialogBox(hInst, /* current instance */
- "Auth_Dialog", /* resource to use */
- hWnd, /* parent handle */
- lpProcDialog); /* Dialog() instance address */
-
- FreeProcInstance(lpProcDialog);
- authenticate(hWnd,hostname,checksum);
- if (sock)
- soclose(sock);
- break;
- }
- else /* Lets Windows process it */
- return (DefWindowProc(hWnd, message, wParam, lParam));
-
- case WM_DESTROY: /* message: window being destroyed */
- if (sock)
- soclose(sock);
- PostQuitMessage(0);
- break;
-
- default: /* Passes it on if unproccessed */
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- return (NULL);
- }
-
-
- /****************************************************************************
-
- FUNCTION: Dialog(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages for "Dialog" dialog box
-
- MESSAGES:
-
- WM_INITDIALOG - initialize dialog box
- WM_COMMAND - Input received
-
- COMMENTS:
-
- No initialization is needed for this particular dialog box, but TRUE
- must be returned to Windows.
-
- Wait for user to click on "Ok" button, then close the dialog box.
-
- ****************************************************************************/
-
- BOOL FAR PASCAL Dialog(hDlg, message, wParam, lParam)
- HWND hDlg; /* window handle of the dialog box */
- unsigned message; /* type of message */
- WORD wParam; /* message-specific information */
- LONG lParam;
- {
- switch (message) {
- case WM_INITDIALOG: /* message: initialize dialog box */
- return (TRUE);
-
- case WM_COMMAND: /* message: received a command */
- if (wParam == IDD_OK || wParam == IDD_CANCEL) {
- if(wParam == IDD_OK) {
- GetDlgItemText(hDlg,IDD_HOST,(LPSTR)hostname,99);
- checksum=(int)GetDlgItemInt(hDlg,IDD_CKSUM,NULL,TRUE);
- } else
- *hostname='\0';
- EndDialog(hDlg, TRUE); /* Exits the dialog box */
- return (TRUE);
- }
- break;
- }
- return (FALSE); /* Didn't process a message */
- }
- /*
- * $Source: /mit/kerberos/src/appl/sample/RCS/sample_client.c,v $
- * $Author: jtkohl $
- *
- * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
- *
- * For copying and distribution information,
- * please see the file <mit-copyright.h>.
- *
- * sample_client:
- * A sample Kerberos client, which connects to a server on a remote host,
- * at port "sample" (be sure to define it in /etc/services)
- * and authenticates itself to the server. The server then writes back
- * (in ASCII) the authenticated name.
- *
- * Usage:
- * sample_client <hostname> <checksum>
- *
- * <hostname> is the name of the foreign host to contact.
- *
- * <checksum> is an integer checksum to be used for the call to krb_mk_req()
- * and mutual authentication
- *
- * If DEBUG is defined, authenticate to server "test.test".
- */
-
-
- #define SAMPLE_SERVICE "sample"
-
- #ifdef DEBUG
- #define TEST_SERVICE "test"
- #endif
-
- static void farcopy(LPBYTE src,LPBYTE dst, int length)
- {
- int i;
-
- for (i=length; i; i--)
- *dst++=*src++;
- }
-
- void authenticate(HWND hWnd,char *hostname,long cksum)
- {
- HANDLE hsp,hhp;
- struct servent far *sp;
- struct hostent far *hp;
- struct sockaddr_in sin, lsin;
- char *remote_host;
- int status;
- int namelen;
- KTEXT_ST ticket;
- char buf[512];
- long authopts;
- MSG_DAT msg_data;
- CREDENTIALS cred;
- Key_schedule sched;
- long host_ip;
- LPSTR env;
-
- set_krb_debug(1);
- set_ap_req_debug(1);
-
- /* clear out the structure first */
- (void) bzero((char *)&sin, sizeof(sin));
-
- /* find the port number for knetd */
- hsp = getservbyname(SAMPLE_SERVICE, "tcp");
- if (!hsp) {
- printf(
- (LPSTR)"unknown service %s/tcp; check /etc/services\n",
- (LPSTR)SAMPLE_SERVICE);
- return;
- }
- sp=(struct servent far*)GlobalLock(hsp);
- /* copy the port number */
- sin.sin_port = sp->s_port;
- GlobalUnlock(hsp);
- GlobalFree(hsp);
- sin.sin_family = AF_INET;
- #if 1
- /* look up the server host */
- hhp = gethostbyname(hostname);
- if (!hhp) {
- printf((LPSTR) "unknown host %s\n",(LPSTR)hostname);
- return;
- }
- hp=(struct hostent far*)GlobalLock(hhp);
- /* copy the hostname into non-volatile storage */
- remote_host = malloc(lstrlen(((LPSTR)hp)+hp->h_name) + 1);
- (void) lstrcpy((LPSTR)remote_host, ((LPSTR)hp)+hp->h_name);
-
- /* set up the address of the foreign socket for connect() */
- sin.sin_family = hp->h_addrtype;
- farcopy(((LPBYTE)hp)+hp->h_addr,
- (LPBYTE)&sin.sin_addr,
- hp->h_length);
- #else /* DNS version */
- printf("Before rhost\n");
- /* copy the hostname into non-volatile storage */
- remote_host = malloc(strlen(hostname + 1);
- (void) strcpy(remote_host, hostname);
- host_ip=rhost(&hostname);
- sin.sin_family=AF_INET;
- sin.sin_addr.s_addr=host_ip;
- #endif
- GlobalUnlock(hhp);
- GlobalFree(hhp);
- /* open a TCP socket */
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- printf((LPSTR)"socket : Error %d\n",GetErrno());
- return;
- }
-
- /* connect to the server */
- if (connect(sock,(struct sockaddr*) &sin, sizeof(sin)) < 0) {
- printf((LPSTR)"connect : Error %d\n", GetErrno());
- return;
- }
-
- /* find out who I am, now that we are connected and therefore bound */
- namelen = sizeof(lsin);
- if (getsockname(sock, (struct sockaddr *) &lsin, &namelen) < 0) {
- printf((LPSTR)"getsockname : error %d\n", GetErrno());
- return;
- }
-
- /* call Kerberos library routine to obtain an authenticator,
- pass it over the socket to the server, and obtain mutual
- authentication. */
-
- authopts = KOPT_DO_MUTUAL;
- status = krb_sendauth(authopts, sock, &ticket,
- #ifdef DEBUG
- TEST_SERVICE, TEST_SERVICE,
- #else
- SAMPLE_SERVICE, remote_host,
- #endif
- NULL, cksum, &msg_data, &cred,
- &sched[0], &lsin, &sin, "VERSION9");
- if (status != KSUCCESS) {
- printf((LPSTR) "%s: cannot authenticate to server: %s\n",
- (LPSTR)"win_clnt", (LPSTR)get_krb_err_txt()[status]);
- return;
- }
-
- /* After we send the authenticator to the server, it will write
- back the name we authenticated to. Read what it has to say. */
- status = soread(sock, buf, 512);
- if (status < 0) {
- printf((LPSTR)"read : Error %d", GetErrno);
- return;
- }
-
- /* make sure it's null terminated before printing */
- if (status < 512)
- buf[status] = '\0';
- MessageBox(hWnd,(LPSTR)buf,(LPSTR)"Server Reply",
- MB_OK|MB_ICONINFORMATION);
-
- }
-
-